package com.ykl.demo.comp.impl;

import com.ykl.base.comp.AbstractJpaEntityComp;
import com.ykl.base.jpa.ExecSqlComp;
import com.ykl.demo.comp.SpcStationComp;
import com.ykl.demo.entity.SpcStation;
import com.ykl.demo.model.StationRoomStats;
import com.ykl.demo.repo.SpcStationRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Slf4j
@Component
public class SpcStationCompImpl extends AbstractJpaEntityComp<SpcStation> implements SpcStationComp {

    private SpcStationRepo mmRepo;

    @Autowired
    private ExecSqlComp mmCompSql;

    protected SpcStationCompImpl(SpcStationRepo repo) {
        super(repo);
        this.mmRepo = repo;
    }

    @Override
    public List<StationRoomStats> stats(String id) {
        String sql = "SELECT class_id, type_id, count(*) as count FROM ykl.spc_room " +
                " where station_id = ?1" +
                " group by  class_id, type_id " +
                " order by  class_id, type_id";
        return mmCompSql.exec(sql,StationRoomStats.class, id);
    }

    @Override
    public Optional<SpcStation> findByStationName(String stationName) {
        return mmRepo.findByChinaName(stationName);
    }

    @Override
    public SpcStation createStationByName(String stationName) {
        SpcStation st = new SpcStation();
        st.setChinaName(stationName);
        return save(st);
    }
}